#include "media/ui/MediaGui.h"

MediaWin::MediaWin(QMap<QString, QMap<QString, QString>> &RGLRLSMAP,
                   QSqlDatabase *_DATABASE_,
                   QMap<QString, QString> &QSSMAP)
    : Request(impact_thread)
    , mediaModeType(MEDIAPLMD::PLMD_LOOP)
     {
    themeset(theme::dark);
    Query = new QSqlQuery(*_DATABASE_);
    __RLMAP__ = &RGLRLSMAP;
    __QSSMAP__ = &QSSMAP;
    DATABASE = _DATABASE_;
    qRegisterMetaType<QPair<bool, NT::WIN_QUIT_ATTRIBUTE>>("QPair<bool, OptimizationWinQuite>");
    d_cursor(Qt::PointingHandCursor);
    impact_str["mdrs-owner"] = LOCAL_OWNER;
    impact_ststr["timbres"] = QStringList({"流畅音质", "标准音质", "高品音质", "无损音质"});
    impact_ststr["fm-video"] = format_video;
    impact_ststr["fm-ovall"] = format_overall;
    impact_ststr["fm-image"] = format_image;
    impact_ststr["fm-lyric"] = foarmat_lyric;

    impact_identify["mdrs-table-type"] = NT::CUT_MUSIC_TYPE;

    impact_int["mdrs-indexof"] = 0;
    impact_int["mdrs-video-index"] = 0;
    impact_int["mdrs-mode-pixmap"] = 1;
    impact_int[RPLKEY.UPD_THREAD_STATE] = NT::THREAD_RECOVER;

    impact_font["table-font"] = RT_mediaTabWidget->font();

    impact_thread[ThreadPoint::MULTI_MDCVUPDATE] = NT::THREAD_IDLEABOUT;
    impact_thread[ThreadPoint::MULTI_NWRQST] = NT::THREAD_IDLEABOUT;
    impact_thread[ThreadPoint::MULTI_TABUPDATE] = NT::THREAD_IDLEABOUT;
    impact_thread[ThreadPoint::NETWORK_VISIT] = NT::THREAD_IDLEABOUT;
    impact_thread[ThreadPoint::MULTI_REMOVE] = NT::THREAD_IDLEABOUT;
    impact_thread[ThreadPoint::MULTI_ADDTION] = NT::THREAD_IDLEABOUT;
    impact_thread[ThreadPoint::MULTI_AMEND] = NT::THREAD_IDLEABOUT;
    impact_thread[ThreadPoint::MULTI_CROP] = NT::THREAD_IDLEABOUT;

    //QTIMER
    GLOBAL_SYSTEM_QUIT_QTIER.setInterval(200);
    GLOBAL_SYSTEM_QUIT_QTIER.setSingleShot(false);
    USER_DLTXTC_QTIER.setInterval(700);
    USER_DLTXTC_QTIER.setSingleShot(true);
    USER_DLLD_QTIER.setInterval(100);
    USER_DLLD_QTIER.setSingleShot(true);
    MULTI_VOLUME_QTIER.setInterval(1000);
    MULTI_VOLUME_QTIER.setSingleShot(true);
    GLOBAL_ARGUR_QTIER.setInterval(50);
    GLOBAL_ARGUR_QTIER.setSingleShot(true);
    TAB_DLLD_QTIER.setInterval(400);
    TAB_DLLD_QTIER.setSingleShot(true);
    MULTI_DLPL_QTIER.setInterval(200);
    MULTI_DLPL_QTIER.setSingleShot(true);

    //ROOF
    LD_cheduleBar->setFixedSize(450,30);
    NW_matchComboBox->setFixedSize(190, 27);

    RTP_addHint->setFixedHeight(25);
    RTP_mainHint->setFixedHeight(25);
    RTP_doallplay->setFixedSize(120,31);

    RTP_musicLabel->setFixedHeight(30);
    RTP_moviesLabel->setFixedHeight(30);
    RTP_TbmatchComboBox->setFixedSize(150,27);

    RTP_removeLabel->setFixedHeight(30);
    RTP_update->setFixedHeight(30);
    RTP_menuLabel->setFixedHeight(30);

    //BOTTOM
    MT_mdplermode->setFixedSize(30, 30);
    MT_volumeView->setFixedSize(30, 30);
    MT_speedView->setFixedSize(30,30);
    MT_screen->setFixedSize(30,30);
    MT_duration->setFixedHeight(23);
    MT_medialast->setFixedSize(27, 20);
    MT_mediastat->setFixedSize(35, 35);
    MT_medianext->setFixedSize(27, 20);
    //setMinimumWidth 重要代码，进度条和音量调节组件各装在
    // 一个布局layout中的，加上最小宽可避免被其他布局压缩
    MT_tracksCover->setFixedSize(BT_mainWidget->height(), BT_mainWidget->height());
    MT_tracksSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    MT_tracksEcho->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

    //Account - layout
    Login_head->setFixedSize(60,60);
    Login_idLibox->setFixedSize(265, 36);
    Login_keyLine->setFixedSize(265, 36);
    Login_sumbit->setFixedSize(265, 30);

    Register_idLine->setFixedSize(265,30);
    Register_keyLine->setFixedSize(265,30);
    Register_QkeyLine->setFixedSize(265,30);
    Register_aliasLine->setFixedSize(265,30);
    Register_verifyWin->setFixedHeight(30);
    Register_verifyinput->setFixedHeight(30);
    Register_QkeyLine->setFixedHeight(30);
    Register_sumbit->setFixedSize(265,30);

    Datum_head->setFixedHeight(120);
    Datum_idLine->setFixedSize(265,30);
    Datum_keyLine->setFixedSize(265,30);
    Datum_QkeyLine->setFixedSize(265,30);
    Datum_aliasEdit->setFixedSize(265,30);
    Datum_historyBox->setFixedHeight(30);
    Datum_register->setFixedHeight(31);
    Datum_submit->setFixedHeight(31);
    Datum_inner->setFixedWidth(280);
    LF_localMeidaLabel->setFixedSize(125, 33);
    LF_nearMeidaLabel->setFixedSize(125, 33);
    LF_recordLabel->setFixedSize(125, 33);
    LF_collectLabel->setFixedWidth(58);
    LD_cheduhint->setFixedHeight(30);

    Favora_ListWidget->setMaximumWidth(124);
    Favora_addtionLabel->setFixedHeight(23);
    Favora_removeLabel->setFixedHeight(23);

    Favora_ListWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
    MT_medialast->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
    MT_mediastat->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
    MT_medianext->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);

    Login_keyLine->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    Register_idLine->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    Register_keyLine->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    Register_QkeyLine->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    Register_aliasLine->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    Register_verifyinput->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    Register_QkeyLine->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    Datum_idLine->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    Datum_keyLine->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    Datum_QkeyLine->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    Datum_aliasEdit->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);

    Login_sumbit->setStyleSheet((*__QSSMAP__)["mpl_stdbt"]);
    Register_sumbit->setStyleSheet((*__QSSMAP__)["mpl_stdbt"]);
    Datum_register->setStyleSheet((*__QSSMAP__)["mpl_stdbt"]);
    Datum_submit->setStyleSheet((*__QSSMAP__)["mpl_stdbt"]);

    QString cheboxstyle = definfc.QssAttributeValueSwap((*__QSSMAP__)["mpl_checkbox"], {"QCheckBox"}, "color", "rgb(242,241,247)");
    Login_autoLogincBox->setStyleSheet(cheboxstyle.arg(":/PIXMAP/bench/sgbox-ckst-w.png", ":/PIXMAP/bench/sgbox-cked-w.png", ":/PIXMAP/bench/sgbox-ckhv-w.png"));
    Login_fillkeycBox->setStyleSheet(cheboxstyle.arg(":/PIXMAP/bench/sgbox-ckst-w.png", ":/PIXMAP/bench/sgbox-cked-w.png", ":/PIXMAP/bench/sgbox-ckhv-w.png"));

    RT_mediaTabWidget->setStyleSheet((*__QSSMAP__)["mpl_tab"].arg("rgb(87,87,87)"));
    RT_mediaTabWidget->horizontalHeader()->setStyleSheet((*__QSSMAP__)["mpl_tabhead"]);
    RT_mediaTabWidget->verticalHeader()->setStyleSheet((*__QSSMAP__)["mpl_tabhead"]);
    NW_blackBoard->priview()->setStyleSheet((*__QSSMAP__)["mpl_tab"].arg("rgb(242,241,247)"));

    Favora_ListWidget->setStyleSheet((*__QSSMAP__)["mpl_listwidget"].arg("transparent", "rgb(254,214,214)"));
    //"""---------global layout---------"""

    RectOverallGrid->addWidget(LF_mainWidget, 0, 0, 14, 1);
    RectOverallGrid->addWidget(RT_mainWidget, 0, 1, 14, 18);
    RectOverallGrid->addWidget(BT_mainWidget, 15, 0, 1, 19);
    RectOverallGrid->setHorizontalSpacing(0);
    RectOverallGrid->setVerticalSpacing(0);

    RectOverallGrid->setContentsMargins(0, 0, 0, 0);
    LF_mainVbox->setContentsMargins(3, 10, 0, 10);
    RTP_mainHbox->setContentsMargins(10, 5, 10, 5);

    RT_sliderShow->addWidget(RT_tabWidget);
    RT_sliderShow->addWidget(Login_Widget);
    RT_sliderShow->addWidget(Register_Widget);
    RT_sliderShow->addWidget(Datum_Widget);
    RT_sliderShow->addWidget(RT_lyricLabel);
    RT_sliderShow->addWidget(NW_blackBoard);
    RT_sliderShow->addWidget(LD_chedulewin);

    RT_sliderShow->setCurrentWidget(RT_tabWidget);
    RectOverallWidget->setAttribute(Qt::WA_TranslucentBackground, false);
    RectOverallWidget->setAttribute(Qt::WA_NoSystemBackground, false);
    RectOverallWidget->update();
    setCentralWidget(RectOverallWidget);

    d_margin_radius(QMargins(3, 0, 3, 3), 4);
    d_filler_enable(NTitleBar::BACKCOLOR, true); //可选背景色和背景图两个模式
    d_filler_color(QColor(248,248,255)); //背景色
    d_painter_effectrange(QRect(0, 0, LF_mainWidget->width(), 0), {QColor(248,248,255)}); //限制渐变区域，x,y表示坐标，width和height则表面宽高，数值为0时表面沿用窗口高或者宽
    d_floating_pixmap(":/PIXMAP/bench/music-floating.jpg"); //悬浮窗背景图
    captionBar()->setFixedHeight(40); //设置标题栏高，默认是28
    captionBar()->d_filler_enable(NTitleBar::BACKCOLOR, true);
    captionBar()->d_filler_color(QColor(248,248,255));
    captionBar()->piazze()->setCentralWidget(RF_additionalWidget); //可以往里面加一个组件，位置再标题栏文字后面
    captionBar()->d_caption("未登录", QFont("PMingLiu", 10), QColor(120,120,120)); //设置标题栏文字
    captionBar()->d_captionico(":/PIXMAP/bench/scale4.ico");
    captionBar()->d_filler_enable(NTitleBar::BACKCOLOR, true); //设置标题栏填充为背景色模式
    captionBar()->d_basicbuttom_freepixmap(NTitleBar::NORWIN, 5, qRgb(49,49,49));
    captionBar()->d_basicbuttom_freepixmap(NTitleBar::MINWIN, 5, qRgb(49,49,49));
    captionBar()->d_basicbuttom_freepixmap(NTitleBar::MAXWIN, 5, qRgb(49,49,49));
    captionBar()->d_basicbuttom_freepixmap(NTitleBar::QUITE,  5, qRgb(49,49,49));
    captionBar()->d_basicbuttom_configur(NT::PIXLOAD_IGNORERATIO_LOWER, NT::DRAWTYPE_RECT, 8, 1.5);
    captionBar()->d_basicbuttom_where({NTitleBar::Rroof_Normal,
                                       NTitleBar::Rroof_Min,
                                       NTitleBar::Rroof_Max,
                                       NTitleBar::Rroof_Exit},
                                      {QString(MSEVKEY.WIN_NORMAL),
                                       QString(MSEVKEY.WIN_LEAVE),
                                       QString(MSEVKEY.WIN_PRESS),
                                       QString(MSEVKEY.WIN_RELEASE)});

    QString rules_width = (*__RLMAP__)["MSC"]["WindownGeometryW"];
    QString rules_height = (*__RLMAP__)["MSC"]["WindownGeometryH"];
    bool check = (!rules_width.isEmpty() && !rules_height.isEmpty() && definfc.isInt(rules_width) && definfc.isInt(rules_height));
    check ? resize(rules_width.toUInt(), rules_height.toUInt()) : resize(1020, 630);
    setVisible(true);

    BoilPool = new DialPool(impact_thread,
                            RGLRLSMAP,
                            impact_str,
                            impact_swtc,
                            impact_int,
                            impact_ststr,
                            impact_stint,
                            impact_identify,
                            DATABASE);
    BoilPool->setMaxThreadCount(QThread::idealThreadCount());

    /********************preparetory dispose***********************/
    Login_Widget->update_pixmap(RT_sliderShow->size());
    Register_Widget->update_pixmap(RT_sliderShow->size());
    Datum_Widget->update_pixmap(RT_sliderShow->size());
    NW_blackBoard->d_background_config(1, RT_sliderShow->size());
    MT_MediaPlayer->setVideoOutput(RT_videoWidget->video_widget);qDebug() << RT_sliderShow->size();
    diary("initialize finish", Diary::Warning)

};

MediaWin::~MediaWin(){
    delete BoilPool;
    delete Query;
    delete BT_temporary;
    delete LF_mainWidget;
    delete NW_blackBoard;
    delete WaitTimer;
}


/*typedef struct tagMSG {
  HWND   hwnd;
  UINT   message;
  WPARAM wParam;
  LPARAM lParam;
  DWORD  time;
  POINT  pt;
  DWORD  lPrivate;
} MSG, *PMSG, *NPMSG, *LPMSG;*/

